<?php
/*
 * article class
 */
namespace lightblog\admin\controller;

use lightblog\engine\controller;
use lightblog\admin\model\authorizeModel;

class authorize extends controller {
    function loginAction() {
        if (\lightblog::$app->request->method == 'POST') {
            $this->doLogin();
        }

        return $this->render();
    }

    private function doLogin() {
        $model = new authorizeModel;

        if ($model->login()) {
            header('location: ' . route('index', 'index'));
        } else {
            header('location: ' . route('authorize', 'authorizeFailed'));
        }
    }

    function logoutAction() {
        $model = new authorizeModel;
        if ($model->logout()) {
            header('location: ' . route('authorize', 'login'));
        } else {
            header('location: '. route('authorize', 'logoutSuccess'));
        }
    }

    function authorizeFailedAction() {
        $data['msg'][] = "Authorize failed.";
        $data['msg'][] = "Please check your username and password.";
        $data['goto'] = route('authorize', 'login');
        return $this->renderFile('msg', $data);
    }

    function logoutSuccessAction() {
        $data['msg'] = "Logout Success";
        $data['goto'] = route('authorize', 'login');
        return $this->render('msg', $data);
    }
}
